-----------------------------------------------------------------------------
--
-- Module      :  MenuHandlers
-- Copyright   :  (c) Lakshmi Narasimhan
-- License     :  GNU-GPL
--
-- Maintainer  :  <lakshminaras2002 at gmail dot com>
-- Stability   :  provisional
-- Portability :  portable
--
--
--
---------------------------------------------------------------------------------
module MenuHandlers(
	setupButtonHandlers,
	setupMenuHandlers)
	where

import Graphics.UI.Gtk
import Graphics.UI.Gtk.Glade
import Common

opButtonLabels =  ["bOpplus", "bOpminus", "bOpmul", "bOpdiv","bOpExp", "bOpPercent", "bOpenPara",
        "bClosedPara", "bOpLog10", "bOpLn", "bOpe","bOpx2", "bOpx3"]

digitButtonLabels = ["bDigit1", "bDigit2", "bDigit3", "bDigit4" ,"bDigit5" ,"bDigit6", "bDigit7" ,"bDigit8",
 "bDigit9", "bDigit0","bDot"]

setupMenuHandlers :: GladeXML -> IO ()
setupMenuHandlers glade_xml = do
    -- set the quit handler,we may want to look for file to be saved
    quitMenuItem <- xmlGetWidget glade_xml castToMenuItem "fileQuitItem"
    quitMenuItem `onActivateLeaf` (on_quit_activate)

    -- set the clear recent list handler

    -- setting the about handler
    mainWindow <- xmlGetWidget glade_xml castToWindow "wGhcalc"
    helpAboutItem <-  xmlGetWidget glade_xml castToMenuItem "helpAboutItem"
    helpAboutItem `onActivateLeaf` (showAboutDialog mainWindow)
    return ()

setupButtonHandlers :: GladeXML -> IO ()
setupButtonHandlers glade_xml = do
    setupDigitHandlers glade_xml
    setupOpHandlers glade_xml
    return ()

setupDigitHandlers :: GladeXML -> IO ()
setupDigitHandlers glade_xml = do
    formulaEntry <- xmlGetWidget glade_xml castToEntry "tFormula"
    digitButtons <- mapM (xmlGetWidget glade_xml castToButton) digitButtonLabels
    mapM (\button -> onClicked button (textEntrySetter formulaEntry button)) digitButtons
    return ()

setupOpHandlers :: GladeXML -> IO ()
setupOpHandlers   glade_xml = do
    formulaEntry <- xmlGetWidget glade_xml castToEntry "tFormula"
    opButtons <- mapM (xmlGetWidget glade_xml castToButton) opButtonLabels

    mapM (\button -> onClicked button (textEntrySetter formulaEntry button)) opButtons

    -- evaluate expression on clicking of = and set the result entry
    bEnter <- xmlGetWidget glade_xml castToButton "bEnter"
    resultEntry <- xmlGetWidget glade_xml castToEntry "tResultEntry"
    errorLabel <- xmlGetWidget glade_xml castToLabel "lErrorStatus"
    
    onClicked bEnter (eqHandler formulaEntry resultEntry errorLabel bEnter)

    --clear the text entries when Clear Button is clicked
    -- TODO set the focus to the formula entry
    bClear <- xmlGetWidget glade_xml castToButton "bClear"
    onClicked bClear (clearHandler formulaEntry resultEntry)

-- Handle backspace button
    bBackspace <- xmlGetWidget glade_xml castToButton "bBackspace"
    onClicked bBackspace (backspaceHandler formulaEntry)
    return ()

textEntrySetter formulaEntry button = do
    label <- buttonGetLabel button
    case label of
        "e^x" -> getSetTextEntry formulaEntry (\x -> x ++ "euler")
        "x^2" -> getSetTextEntry formulaEntry (\x -> x ++ "^2")
        "x^3" -> getSetTextEntry formulaEntry (\x -> x ++ "^3")
        _ -> getSetTextEntry formulaEntry (\x -> x ++ label)
    return ()

paraHandler formulaEntry button = do
  -- we can do realtime error
    para <- buttonGetLabel button
    getSetTextEntry formulaEntry (\x -> x ++ para)  				
    return ()

-- FIXME too many parameters ?
eqHandler formulaEntry resultEntry errorLabel button = do
    evaluateAndSetResult formulaEntry resultEntry errorLabel
    return()


clearHandler formulaEntry resultEntry = do
    clearTextEntries [formulaEntry, resultEntry]
    return ()

getSetTextEntry entry func = do
    entryText  <- entryGetText entry
    entrySetText entry (func entryText)

backspaceHandler formulaEntry = do
  getSetTextEntry formulaEntry (\x -> take (length x - 1) x)
  return ()

clearTextEntries entryList = do
    mapM (flip entrySetText "") entryList
    return ()

on_quit_activate = do
              mainQuit
              return ()

showAboutDialog parent = do
    aboutDialog <- aboutDialogNew
    set aboutDialog [
      aboutDialogName      := "GhCalc",
      aboutDialogVersion   := "0.1",
      aboutDialogCopyright := "Lakshmi Narasimhan Vaikuntam",
      aboutDialogComments  := "A calculator with expression editor",
      aboutDialogWebsite   := "http://code.google.com/p/ghcalc"
      ]
    windowSetTransientFor aboutDialog parent
    afterResponse aboutDialog $ \_ -> widgetDestroy aboutDialog
    widgetShow aboutDialog
